﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using DepInnerTask.Common.Interface;
using DepInnerTask.Common.Entity;
using Common.Service;
namespace Ultimus.Web
{
    public partial class LogException : System.Web.UI.Page
    {
        IExceptionLog log = ServiceContainer.GetService<IExceptionLog>();
        ITasks task = ServiceContainer.GetService<ITasks>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropList();
                BindDropListFish();
                Index = "0";
                LoadpageDate(0);
                LoadpageDate1(0);
                //判断是否流程管理方  如果是则 可以新增 否则隐藏新增按钮

            }
        }
        public string Index { get; set; }
        #region 绑定所有流程
        private void BindDropList()
        {
            this.ddlProcessCatalog.DataSource = task.GetList();
            this.ddlProcessCatalog.DataTextField = "PROCESSNAME";
            this.ddlProcessCatalog.DataValueField = "TASKID";
            this.ddlProcessCatalog.DataBind();
            this.ddlProcessCatalog.Items.Insert(0, new ListItem("所有...", "0"));
        }
        #endregion
        #region 绑定所有流程
        private void BindDropListFish()
        {
            this.drpListProcess.DataSource = task.GetList();
            this.drpListProcess.DataTextField = "PROCESSNAME";
            this.drpListProcess.DataValueField = "TASKID";
            this.drpListProcess.DataBind();
            this.drpListProcess.Items.Insert(0, new ListItem("所有...", "0"));
        }
        #endregion
        private void LoadpageDate(int index)
        {
            int pageSize = 10;
            int count = 0;
            string whereStr = "";
            if (!string.IsNullOrEmpty(processName.Value))
            {
                whereStr += "TITLE▽" + processName.Value + "△";
            }
            if (!string.IsNullOrEmpty(bussinessID.Value))
            {
                whereStr += "DOCUMENT_NUMBER▽" + bussinessID.Value + "△";
            }
            if (!string.IsNullOrEmpty(applicantUser.Value))
            {
                whereStr += "ACTION_BY▽" + applicantUser.Value + "△";
            }
            if (!String.IsNullOrEmpty(startDate.Value))
            {
                whereStr += "ACTION_DATE▽" + startDate.Value + "△";
            }
            if (!String.IsNullOrEmpty(endDate.Value))
            {
                whereStr += "CONFIRM_DATE▽" + endDate.Value + "△";
            }
            if (ddlProcessCatalog.SelectedValue != "0")
            {
                whereStr += "ProcessNAME▽" + ddlProcessCatalog.SelectedValue + "△";
            }
            if (ddlTaskStatus.SelectedValue != "-1")
            {
                whereStr += "EXCEPTION_TYPE▽" + ddlTaskStatus.SelectedValue + "△";
            }
            if (!String.IsNullOrEmpty(StoDep.Value))
            {
                whereStr += "ACTION_DEPARTMENT▽" + StoDep.Value + "△";
            }
            if (!String.IsNullOrEmpty(StoDepTask.Value))
            {
                whereStr += "ILLEGAL_DEPARTMENT_NAME▽" + StoDepTask.Value + "△";
            }
            if (!String.IsNullOrEmpty(StopUser.Value))
            {
                whereStr += "CONFIRM_BY▽" + StopUser.Value + "△";
            }
            if (ddlIsWei.SelectedValue != "-1")
            {
                whereStr += "IS_ILLEGAL▽" + ddlIsWei.SelectedValue + "△";
            }
            IList list = log.GetList(whereStr, 0, 10, out count,"1");
            PagedDataSource page = new PagedDataSource();

            int totalCount = list.Count;
            //DataView dv = dt.DefaultView;
            page.DataSource = list;
            page.AllowPaging = true;
            page.PageSize = pageSize;
            page.CurrentPageIndex = index;

            rpMyTask.DataSource = page;
            rpMyTask.DataBind();

            pagination.RecordCount = totalCount;
            pagination.PageSize = page.PageSize;
            pagination.CurrentPageIndex = page.CurrentPageIndex + 1;
            pagination.Visible = true;
            pagination.AlwaysShow = true;
            pagination.CurrentPageButtonClass = "btnPageNavCurrentInput";
            pagination.DataBind();
        }

        private void LoadpageDate1(int index)
        {
            int pageSize = 10;
            int count = 0;
            string whereStr = "";
            if (!string.IsNullOrEmpty(processName.Value))
            {
                whereStr += "TITLE▽" + processName.Value + "△";
            }
            if (!string.IsNullOrEmpty(bussinessID.Value))
            {
                whereStr += "DOCUMENT_NUMBER▽" + bussinessID.Value + "△";
            }
            if (!string.IsNullOrEmpty(applicantUser.Value))
            {
                whereStr += "ACTION_BY▽" + applicantUser.Value + "△";
            }
            if (!String.IsNullOrEmpty(startDate.Value))
            {
                whereStr += "ACTION_DATE▽" + startDate.Value + "△";
            }
            if (!String.IsNullOrEmpty(endDate.Value))
            {
                whereStr += "CONFIRM_DATE▽" + endDate.Value + "△";
            }
            if (ddlProcessCatalog.SelectedValue != "0")
            {
                whereStr += "ProcessNAME▽" + ddlProcessCatalog.SelectedItem.Text + "△";
            }
            if (ddlTaskStatus.SelectedValue != "-1")
            {
                whereStr += "EXCEPTION_TYPE▽" + ddlTaskStatus.SelectedItem.Text + "△";
            }
            if (!String.IsNullOrEmpty(StoDep.Value))
            {
                whereStr += "ACTION_DEPARTMENT▽" + StoDep.Value + "△";
            }
            if (!String.IsNullOrEmpty(StoDepTask.Value))
            {
                whereStr += "ILLEGAL_DEPARTMENT_NAME▽" + StoDepTask.Value + "△";
            }
            if (!String.IsNullOrEmpty(StopUser.Value))
            {
                whereStr += "CONFIRM_BY▽" + StopUser.Value + "△";
            }
            if (ddlIsWei.SelectedValue != "-1")
            {
                whereStr += "IS_ILLEGAL▽" + ddlIsWei.SelectedValue + "△";
            }
            IList list = log.GetList(whereStr, 0, 10, out count,"2");
            PagedDataSource page = new PagedDataSource();

            int totalCount = list.Count;
            //DataView dv = dt.DefaultView;
            page.DataSource = list;
            page.AllowPaging = true;
            page.PageSize = pageSize;
            page.CurrentPageIndex = index;

            rptNo.DataSource = page;
            rptNo.DataBind();

            pagination.RecordCount = totalCount;
            pagination.PageSize = page.PageSize;
            pagination.CurrentPageIndex = page.CurrentPageIndex + 1;
            pagination.Visible = true;
            pagination.AlwaysShow = true;
            pagination.CurrentPageButtonClass = "btnPageNavCurrentInput";
            pagination.DataBind();
        }
        //修改按钮的单击事件
        //protected void lkbtnDetails_Click(object sender, EventArgs e)
        //{
        //    Index = "0";
        //    //当前点击的按钮
        //    LinkButton lb = (LinkButton)sender;
        //    //获取传过来的CommentId
        //    string comId = lb.CommandArgument;
        //   // OpenNewWinodw("LogExceptionAdd.aspx?EXCEPTION_ID=" + comId, "详细异常日志", 1000, 700, "", "", "yes", "no", true);
        //    lb.Attributes.Add("OnClick", "ShowModalDialog('LogExceptionAdd.aspx?EXCEPTION_ID="+comId+"')");
        //}

        protected void rpMyTask_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //需要判断是否为发起人 只有发起人可以修改 删除 自己的回复信息
                LinkButton lkbtndel = (LinkButton)e.Item.FindControl("lkbtnDetails");
                //获取传过来的CommentId
                string comId = lkbtndel.CommandArgument;
                 //OpenNewWinodw("LogExceptionAdd.aspx?EXCEPTION_ID=" + comId, "详细异常日志", 1000, 700, "", "", "yes", "no", true);
                // lkbtndel.PostBackUrl = "LogExceptionAdd.aspx?EXCEPTION_ID=" + comId;
                lkbtndel.Attributes.Add("OnClick", "ShowModalDialog('LogExceptionAdd.aspx?EXCEPTION_ID=" + comId + "')");
                  string ss = ((ExceptionLogEntity)(e.Item.DataItem)).IS_ILLEGAL;
                  string type = ((ExceptionLogEntity)(e.Item.DataItem)).EXCEPTION_TYPE;//异常类型
                Label lblISLegal = (Label)e.Item.FindControl("lblISLegal");
                Label lblType = (Label)e.Item.FindControl("lblType");
                if (ss == "0")
                {
                    lblISLegal.Text = "否";
                }
                else if(ss=="1")
                {
                    lblISLegal.Text = "是";
                }

                if (type == "0")
                {
                    lblType.Text = "自动终止流程";
                }
                else if(type == "1")
                {
                    lblType.Text = "自动终止任务";
                }
                else if(type == "2")
                {
                    lblType.Text = "经办人手动终止流程";
                }
                else if(type == "3")
                {
                    lblType.Text = "经办人手动终止任务";
                }
                else if(type == "4")
                {
                    lblType.Text = "管理方手动终止流程";
                }
                else if (type == "5")
                {
                    lblType.Text = "管理方手动终止任务";
                }
                hidIndex.Value = "0";
            }

        }

        protected void rptNo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //需要判断是否为发起人 只有发起人可以修改 删除 自己的回复信息
                LinkButton lkbtndel = (LinkButton)e.Item.FindControl("lkbtnDetails1");
                //获取传过来的CommentId
                string comId = lkbtndel.CommandArgument;
                //OpenNewWinodw("LogExceptionAdd.aspx?EXCEPTION_ID=" + comId, "详细异常日志", 1000, 700, "", "", "yes", "no", true);
                // lkbtndel.PostBackUrl = "LogExceptionAdd.aspx?EXCEPTION_ID=" + comId;
                lkbtndel.Attributes.Add("OnClick", "ShowModalDialog('LogExceptionAdd.aspx?EXCEPTION_ID=" + comId + "')");
                  string ss = ((ExceptionLogEntity)(e.Item.DataItem)).IS_ILLEGAL;
                Label lblISLegal = (Label)e.Item.FindControl("lblISLegal1");
                if (ss == "0")
                {
                    lblISLegal.Text = "否";
                }
                else if (ss == "1")
                {
                    lblISLegal.Text = "是";
                }
                hidIndex.Value = "1";
            }

        }
        //public string GetStatusString(string status)
        //{
        //    string str = "";
        //    switch (status)
        //    {
        //        case "0":
        //            str = "否";
        //            break;
        //        case "1":
        //            str = "是";
        //            break;
               
        //        default:
        //            break;
        //    }
        //    return str;
        //}
        /// <summary>
        /// 分页选择
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void pagination_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            LoadpageDate(e.NewPageIndex - 1);
            Index = "0";
        }
        /// <summary>
        /// 分页选择
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            LoadpageDate(e.NewPageIndex - 1);
            Index = "0";
        }
        protected void btnSearch2_Click(object sender, EventArgs e)
        {
            LoadpageDate1(0);
            Index = "1";
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadpageDate(0);
            Index = "0";
        }
        
        #region  打开一个窗口，并且这个窗口位于最前面
        /// <summary>
        /// 打开一个窗口，并且这个窗口位于最前面
        /// </summary>
        /// <param name="Page">提用的页面</param>
        /// <param name="URL">要打开的URL</param>
        /// <param name="PageName">要打开页面的名称</param>
        /// <param name="Win_Width">窗口宽度</param>
        /// <param name="Win_Hight">窗口高度</param>
        /// <param name="Left">窗口左侧位置</param>
        /// <param name="Top">窗口右侧位置</param>
        /// <param name="CenterFlag">是否右中 yes/no</param>
        /// <param name="Status">是否显示状态栏 yes/no</param>
        /// <param name="ParentFlag">true:不关闭弹出窗口，将不能操作父窗口 false 可以操作父窗口</param>
        public void OpenNewWinodw(string URL, string PageName, int Win_Width, int Win_Hight, string Left, string Top, string CenterFlag, string Status, bool ParentFlag)
        {

            string scriptstr = "";
            if (ParentFlag)
            {
                scriptstr = "<script language=javascript>window.showModalDialog('" + URL + "','" + PageName + "','dialogWidth:" + Win_Width + "px;"
                    + "dialogHeight:" + Win_Hight + "px;dialogLeft:" + Left + "px;dialogTop:" + Top + "px;center:" + CenterFlag.ToString() + ";help:no;resizable:yes;status:" + Status + ";scroll:yes')</script>";
            }
            else
            {
                scriptstr = "<script language=javascript>window.showModelessDialog('" + URL + "','" + PageName + "','dialogWidth:" + Win_Width + "px;"
                    + "dialogHeight:" + Win_Hight + "px;dialogLeft:" + Left + "px;dialogTop:" + Top + "px;center:" + CenterFlag.ToString() + ";help:no;resizable:yes;status:" + Status + ";scroll:yes')</script>";
            }
            HttpContext.Current.Response.Write(scriptstr);
        }
        #endregion
    }
}